package fanshe.second.demo04;

import java.lang.reflect.Method;

/**
 * @author Mr.ma
 * @since 2022/10/27 20:30
 **/
public class Test04 {
    public static void main(String[] args) throws Exception {
        doSome();
        doOther();
    }
    public static void doSome() {
        Class<X> xClass = X.class;
        Method[] classDeclaredMethods = xClass.getDeclaredMethods();
        for (Method classDeclaredMethod : classDeclaredMethods) {
            System.out.println(classDeclaredMethod.getName());
        }
        Method[] methods = xClass.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
    public static void doOther() throws Exception {
        X x = new X();
        Class<? extends X> xClass = x.getClass();
        Method y = xClass.getMethod("y", String.class);
        Object yInvoke = y.invoke(x, "父类中的方法回调");
        System.out.println("yInvoke = " + yInvoke);
        Method x1 = xClass.getDeclaredMethod("x");
        Method x2 = xClass.getDeclaredMethod("x", char.class);
        Object invoke = x1.invoke(x);
        System.out.println("invoke = " + invoke);
        x2.setAccessible(true);
        Object c = x2.invoke(x, 'c');
        System.out.println("c = " + c);
    }
}
class X extends Y{
    public String x(String string){
        System.out.println("public===x");
        return string;
    }
    Integer x(Integer integer){
        System.out.println("default===x");
        return integer;
    }
    protected void x(){
        System.out.println("protected===x");
    }
    private char x(char c){
        System.out.println("private===x");
        return c;
    }
}
class Y{
    public String y(String string){
        System.out.println("public===x");
        return string;
    }
    Integer y(Integer integer){
        System.out.println("default===x");
        return integer;
    }
    protected void y(){
        System.out.println("protected===x");
    }
    private char y(char c){
        System.out.println("private===x");
        return c;
    }
}